package com.zzx.controller;

import com.zzx.domain.Vo.AddCategoriesVo;
import com.zzx.exception.ExceptionState;
import com.zzx.exception.SystemException;
import com.zzx.service.CategoryService;
import com.zzx.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询商品分类列表
     * @param type
     * @param pagenum
     * @param pagesize
     * @return
     */
    @PreAuthorize("hasAuthority('商品分类')")
    @GetMapping()
    public ResponseResult getCategoriesList(@RequestParam(required = false)Integer type, @RequestParam(required = false)Integer pagenum,@RequestParam(required = false) Integer pagesize){
        return categoryService.getCategoriesList(type,pagenum,pagesize);
    }

    /**
     * 添加分类
     * @return
     */
    @PreAuthorize("hasAuthority('添加分类')")
    @PostMapping()
    public ResponseResult addCategories(@RequestBody AddCategoriesVo categoriesVo){
        if(Objects.isNull(categoriesVo.getCat_id())) throw new SystemException(ExceptionState.PARENTCATEGORIES);
        if(Objects.isNull(categoriesVo.getCat_name())) throw new SystemException(ExceptionState.CATEGORIESNAME);
        if(Objects.isNull(categoriesVo.getCat_level())) throw new SystemException(ExceptionState.CATEGORIESLEVEL);
        return categoryService.addCategories(categoriesVo);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('获取分类详情')")
    @GetMapping("/{id}")
    public ResponseResult selectCategoriesById(@PathVariable Integer id){
        return categoryService.selectCategoriesById(id);
    }

    /**
     * 根据id修改分类
     * @param id
     * @param addCategoriesVo
     * @return
     */
    @PreAuthorize("hasAuthority('添加分类')")
    @PutMapping("/{id}")
    public ResponseResult updateCategoriesById(@PathVariable Integer id,@RequestBody AddCategoriesVo addCategoriesVo){
        return categoryService.updateCategoriesById(id,addCategoriesVo);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('删除分类')")
    @DeleteMapping("/{id}")
    public ResponseResult deleteCategoriesById(@PathVariable Integer id){
        return categoryService.deleteCategoriesById(id);
    }


}
